package net.huashitong.supervision.gongjian.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.NumberUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import com.sun.org.apache.regexp.internal.RE;
import net.huashitong.supervision.gongjian.dao.*;
import net.huashitong.supervision.gongjian.entity.ProjectFeedBackEntity;
import net.huashitong.supervision.gongjian.entity.ProjectTaskEntity;
import net.huashitong.supervision.gongjian.entity.project.JungongTouchanEntity;
import net.huashitong.supervision.gongjian.entity.project.KaiGongEntity;
import net.huashitong.supervision.gongjian.entity.project.ZaijianTisuEntity;
import net.huashitong.supervision.gongjian.entity.project.ZhaoShangProjectEntity;
import net.huashitong.supervision.gongjian.filter.ProjectFeedBackFilter;
import net.huashitong.supervision.gongjian.service.project.JungongTouchanService;
import net.huashitong.supervision.gongjian.service.project.KaiGongService;
import net.huashitong.supervision.gongjian.service.project.ZaijianTisuService;
import net.huashitong.supervision.gongjian.service.project.ZhaoShangProjectService;
import net.huashitong.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProjectFeedBackService extends StringPKService<ProjectFeedBackEntity> {
    @Autowired
    private ProjectFeedBackDao dao;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private ProjectTaskDao taskDao;
    @Autowired
    private SysUserService userService;
    @Autowired
    private KaiGongDao kaigongdao;
    @Autowired
    private ZaijianTisuDao zaijianTisuDao;
    @Autowired
    private JungongTouchanDao jungongDao;
    @Autowired
    private ZhaoShangProjectDao zhaoshangDao;

    @Override
    protected BaseDao<ProjectFeedBackEntity, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(ProjectFeedBackEntity obj) {

        String sysId = tableService.updateMaxSysId("t_gj_feedback", null);
        obj.setSysId(sysId);
    }

    public List<ProjectFeedBackEntity> getList(String projectId)
    {
        if(StringUtils.isBlank(projectId))
        {
            return  null;
        }
        ProjectFeedBackFilter filter = new ProjectFeedBackFilter();
        filter.setProjectId(projectId);
        List<ProjectFeedBackEntity> list = dao.getList(filter);
        return list;
    }


    /**
     * 反馈
     * @param filter
     */
    public int  saveFeedback(ProjectFeedBackFilter filter){


        String loginId = SysUserUtils.getUserId();
        if(StringUtils.isBlank(loginId)){
            return  2;  //超时
        }
        int expire=0;
        String taskId = filter.getTaskId();
        ProjectTaskEntity projectTask = taskDao.getById(taskId);



        String projectId = projectTask.getProjectId();
        int projectType = projectTask.getProjectType();
        ProjectFeedBackEntity feedback = dao.getFeedback(projectId, taskId,projectTask.getProjectType(),loginId);
        String taskEndDate = projectTask.getTaskEndDate();//本次任务截止日期
        String currentDate = DateUtil.getCurrentDate();

        Date endDate,curDate;
        endDate = DateUtil.strToDate(taskEndDate);
        curDate= DateUtil.strToDate(currentDate);


        int i = endDate.compareTo(curDate);
        if(i<=0){
            //过期 设置过期tainbao状态
//            feedback.setExpire(1);
            expire=1;
        }


        if(ObjectUtils.isBlank(feedback))
        {
            feedback = new ProjectFeedBackEntity();
//            BeanUtils.copyProperties(filter,feedback);

            feedback.setXingxiang(filter.getXingxiang());//形象进度
            feedback.setReachoals(filter.getReachoals());//是否达标 1 是  2 否  0 未填写
            feedback.setTaskId(taskId);
            feedback.setProjectId(projectId);
            feedback.setSysId(null);
            feedback.setRemarks(filter.getRemarks());
            feedback.setAudit(0);
            feedback.setCreateName(SysUserUtils.getusername());
//            feedback.setAddCurrentValue(0);
            feedback.setCurrentValue(filter.getCurrentValue());
            feedback.setTotalValue(filter.getTotalValue());
            feedback.setRemarks(feedback.getRemarks());
            feedback.setExpire(expire);
            switch (projectType)
            {
                case 1:
                    feedback.setProjectType(1);

                    feedback.setCurrentMianji(filter.getCurrentMianji());//本期完成交地面积
                    feedback.setFinishMianji(filter.getFinishMianji());//

                    feedback.setCurrentChaiqian(filter.getCurrentChaiqian());//本期完成拆迁面积
                    feedback.setFinishChaiqian(filter.getFinishChaiqian());//完成拆迁总面积

                    feedback.setCurrentZhengqianhu(filter.getCurrentZhengqianhu());
                    feedback.setFinishZhengqianhu(filter.getFinishZhengqianhu());

                    feedback.setZhengdiAudit(0);
                    feedback.setChaiqianAudit(0);

                    break;
                case 2:
                    feedback.setProjectType(2);
                    feedback.setKaigongState(feedback.getKaigongState());

                    double rate = getRate(projectId, 2,filter.getTotalValue());
                    feedback.setCompletion(rate);

                    break;
                case 3:
                    feedback.setProjectType(3);
                    double zjRate = getRate(projectId, 3,filter.getTotalValue());
                    feedback.setCompletion(zjRate);

                    break;
                case 4:
                    feedback.setProjectType(4);
                    feedback.setJungongState(filter.getJungongState());
                    double jgRate = getRate(projectId, 4,filter.getTotalValue());
                    feedback.setCompletion(jgRate);
                    break;
                case  5:
                    feedback.setProjectType(5);
                    double zsRate = getRate(projectId, 5,filter.getTotalValue());
                    feedback.setCompletion(zsRate);


                    break;

                default :

                    return 3;
            }




            this.save(feedback);
            System.out.println(feedback);
        }else {
            int audit = feedback.getAudit();

            if(1!=projectType&&1==audit)
            {
                return 3;//审核完毕 无法填报
            }
            BeanUtils.copyProperties(filter,feedback,"taskId","projectId","createDate","sysId","projectType","createName");
            double rate = getRate(projectId, projectType, filter.getTotalValue());
            feedback.setCompletion(rate);
            feedback.setExpire(expire);
            this.update(feedback);
        }

        return 1;
    }


    private double getRate(String projectId,int projectType,double total)
    {
        double rate=0,plan=0;
        switch (projectType){
            case 2:
                KaiGongEntity kaiGong = kaigongdao.getById(projectId);
                plan= kaiGong.getGongjianjihua();
                break;
            case 3:
                ZaijianTisuEntity zaijian = zaijianTisuDao.getById(projectId);
                plan=zaijian.getGongjianPlan();
                break;
            case 4:
                JungongTouchanEntity jungong = jungongDao.getById(projectId);
                plan=jungong.getGongjianjihua();
                break;
            case 5:
                ZhaoShangProjectEntity zhaoshang = zhaoshangDao.getById(projectId);
                plan=zhaoshang.getInvestment();
                break;
            default:
                plan=0;
                break;
        }


        rate =total/plan;
        rate  = NumberUtils.doubleFormat(rate, 2);
        rate=rate*100;
        return rate;
    }


    /**
     * 当期是否填报
     */

    public  int getFeedbackCount(String projectId,int vaild ,int projectType){
//        dao
        int feedBackCount = dao.getCurrnetFeedbackCount(projectId, vaild, projectType);

        return feedBackCount;
    }


    public List<ProjectFeedBackEntity> getCurrentFeedBackList(String projectId,int vaild ,int projectType,String createName){
        return dao.getCurrnetFeedbackList(projectId, vaild, projectType,createName);
    }

}
